#include <QSqlQuery>
#include <QMessageBox>
#include "databasesetting.h"
#include <QVariant>
#include <QSqlError>

DataBaseSetting::DataBaseSetting(QObject *parent) :
    QObject(parent)
{
}

bool DataBaseSetting::init(){
    m_dwnTableName = "DownloadData";
    m_dwnTableColumn<<"Filename"<<"Location"<<"Percent"<<"Priority";
    m_dwnTableColumnType<<"TEXT"<<"TEXT"<<"INTEGER"<<"INTEGER";

    m_urlTableName = "UrlData";
    m_urlTableColumn<<"Filename"<<"Location"<<"Url";
    m_urlTableColumnType<<"TEXT"<<"TEXT"<<"TEXT";

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("downloaddatabase.sqlite");

    if(!db.open()){
        return false;
    }

    if(db.tables().size() == 0){
        QSqlQuery query(db);
        query.exec(getCreateDownloadTableQuery());
        query.exec(getCreateUrlTableQuery());
    }
    return true;
}

QString DataBaseSetting::getCreateDownloadTableQuery(){
    QString queryStr = "CREATE TABLE " + m_dwnTableName +"(";
    for(int i = 0; i < m_dwnTableColumn.size(); ++i){
        queryStr.append(m_dwnTableColumn.at(i) + " " + m_dwnTableColumnType.at(i));
        if(i < m_dwnTableColumn.size()-1){
            queryStr.append(", ");
        }
    }
    queryStr.append(")");
    return queryStr;
}

QString DataBaseSetting::getCreateUrlTableQuery(){
    QString queryStr = "CREATE TABLE " + m_urlTableName + "(";
    for(int i = 0; i < m_urlTableColumn.size(); ++i){
        queryStr.append(m_urlTableColumn.at(i) + " " + m_urlTableColumnType.at(i));
        if(i < m_urlTableColumn.size()-1){
            queryStr.append(", ");
        }
    }
    queryStr.append(")");
    return queryStr;
}

void DataBaseSetting::getTaskForDownload(QVector<DownloadTask*> &tasks){
    QSqlQuery query(db);
    QString queryTemplate = "SELECT Filename, Location, Priority FROM %1 "
                            "WHERE Percent < 100";

    query.exec(queryTemplate.arg(m_dwnTableName));
    while(query.next()) {
        QString fileName = query.value(0).toString();
        QString location = query.value(1).toString();
        int priority = query.value(2).toInt();

        QSqlQuery query1(db);
        QString queryTemplate1 = "SELECT Url FROM %1 WHERE Filename='%2' AND Location='%3'";
        query1.exec(queryTemplate1.arg(m_urlTableName)
                                  .arg(fileName)
                                  .arg(location));
        int s = query1.size();
        if(query1.next()){
            QString url = query1.value(0).toString();

            tasks.append(new DownloadTask(QUrl::fromUserInput(url), fileName,
                                          location, priority));
        }
    }
}

QString DataBaseSetting::dwnTableName(){
    return m_dwnTableName;
}

QStringList DataBaseSetting::dwnTableColumn(){
    return m_dwnTableColumn;
}

QStringList DataBaseSetting::dwnTableColumnType(){
    return m_dwnTableColumnType;
}

QString DataBaseSetting::urlTableName(){
    return m_urlTableName;
}

QStringList DataBaseSetting::urlTableColumn(){
    return m_urlTableColumn;
}

QStringList DataBaseSetting::urlTableColumnType(){
    return m_urlTableColumnType;
}

DataBaseSetting::~DataBaseSetting(){
    if(db.isOpen()) db.close();
}
